package com.howtodoinjava.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.howtodoinjava.entity.User;

@Repository
public class UserDAOImpl implements UserDAO {
	@Autowired
	SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Transactional
	public User getUserByName(String userName) {
		System.out.println("sessionFactory : " + sessionFactory);
		System.out.println("session : " + sessionFactory.getCurrentSession());
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(
				"FROM User as user WHERE user.userName = :userName")
				.setParameter("userName", userName);
		return (User) query.uniqueResult();
	}

	public void insertUser(User user) {
		sessionFactory.getCurrentSession().save(user);
	}

	public void updateUser(User user) {
		// TODO Auto-generated method stub

	}

	public void deleteUser(String userName) {
		// TODO Auto-generated method stub

	}

	public void deleteUser(User user) {
		// TODO Auto-generated method stub

	}

}
